package com.orion.config;

import com.orion.token.MyOauth2AccessToken;
import org.springframework.security.oauth2.common.OAuth2AccessToken;
import org.springframework.security.oauth2.provider.OAuth2Authentication;
import org.springframework.security.oauth2.provider.token.TokenEnhancer;

import java.util.HashMap;
import java.util.Map;

/**
 * @author Administrator
 * @date 2021/9/28
 */
public class CustomTokenEnhancer implements TokenEnhancer {
    @Override
    public OAuth2AccessToken enhance(OAuth2AccessToken accessToken, OAuth2Authentication authentication) {

        MyOauth2AccessToken myOauth2AccessToken = new MyOauth2AccessToken(accessToken);

        Map<String, Object> additionalInfo = new HashMap<>(4);
        additionalInfo.put("customInfo", "extra thing额外的东西");

        System.out.println("get authentication in customTokenEnhancer = " + authentication.toString());
        Map<String, Object> additionalInformation = accessToken.getAdditionalInformation();
        System.out.println("accessToken additionalInformation = " + additionalInformation);
        System.out.println("accessToken = " + accessToken);

        additionalInfo.putAll(accessToken.getAdditionalInformation());

        myOauth2AccessToken.setAdditionalInformation(additionalInfo);

        return myOauth2AccessToken;
    }
}
